package com.dq.domain.record;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.domain.orders.Orders;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;

/**
 * @Author luchangtong
 * @Date 2019/11/20 15:54
 */
@Data
@Entity
@ApiModel("提成记录")
public class RoyaltyRecord extends BaseEntity implements VO.ToVO {
    @ApiModelProperty("门店")
    @ManyToOne
    protected SysSection org;
    @ApiModelProperty("员工")
    @ManyToOne
    protected SysUser sysUser;
    @ApiModelProperty("订单")
    @ManyToOne
    protected Orders orders;
    @ApiModelProperty("提成金额")
    protected BigDecimal royaltyPrice;

    @Data
    @ApiModel(value = "提成记录视图")
    public static class Vo implements VO {
        public Long id;
        public Long orgId;
        public Long orderId;
        public String orgName;
        public String staffNo;
        public Long staffUserId;
        public String staffName;
        public String orderNo;
        @ApiModelProperty("最终支付金额")
        protected BigDecimal price;
        @ApiModelProperty("提成金额")
        protected BigDecimal royaltyPrice;
        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "修改时间")
        private String lastModifyTime;
        @ApiModelProperty(value = "创建者id")
        private Long createUserId;
        @ApiModelProperty(value = "创建者name")
        private String createUserName;
        @ApiModelProperty(value = "修改者id")
        private Long updateUserId;
        @ApiModelProperty(value = "修改者name")
        private String updateUserName;
    }
    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.orderId = this.getOrders().getId();
        vo.orgId = this.getOrg().getId();
        vo.orgName = this.getOrg().getSimpleName();
        vo.staffNo = this.sysUser.getCardNo();
        vo.staffUserId = this.sysUser.getId();
        vo.staffName = this.sysUser.getUserRealcName();
        UserOrder userOrder = (UserOrder) this.getOrders();
        vo.orderNo = userOrder.getOrdersNo();
        vo.price = userOrder.getPrice();
        vo.royaltyPrice = this.royaltyPrice;
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.lastModifyTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createUserId = this.createUser == null ? null : this.createUser.getId();
        vo.createUserName = this.createUser == null ? null : this.createUser.getUsername();
        vo.updateUserId = this.updateUser == null ? null : this.updateUser.getId();
        vo.updateUserName = this.updateUser == null ? null : this.updateUser.getUsername();
        return vo;
    }

    @Data
    @ApiModel("提成记录列表视图")
    public static class RoyaltyRecordListVo{
        @ApiModelProperty("订单id")
        protected Long id;
        @ApiModelProperty("销售单号")
        protected String saleOrderNo;
        @ApiModelProperty("岗位名称")
        protected String orgPosition;
        @ApiModelProperty("销售名称")
        protected String saleRealCName;
        @ApiModelProperty("销售金额")
        protected BigDecimal salePrice;
        @ApiModelProperty("提成金额")
        protected BigDecimal royaltyPrice;
    }

    public RoyaltyRecordListVo toRoyaltyRecordListVo(){
        RoyaltyRecord.RoyaltyRecordListVo royaltyRecordListVo = new RoyaltyRecord.RoyaltyRecordListVo();
        UserOrder userOrder = (UserOrder)this.getOrders();
        royaltyRecordListVo.id = userOrder.getId();
        royaltyRecordListVo.saleOrderNo = fmt(userOrder.getOrdersNo());
        StaffUser staffUser = (StaffUser)this.getSysUser();
        royaltyRecordListVo.saleRealCName = fmt(staffUser.getUserRealcName());
        royaltyRecordListVo.salePrice = fmt(userOrder.getPrice());
        royaltyRecordListVo.royaltyPrice = fmt(this.getRoyaltyPrice());
        royaltyRecordListVo.orgPosition = staffUser.getOrgPosition() != null ? staffUser.getOrgPosition().getPostName() : "";
        return royaltyRecordListVo;
    }

}
